<template>
	<view>
		<view id="navbar">
			<u-navbar :is-back="false" title=" ">
				<view class="slot-wrap">
					<view class="search" @click="search()">
						<image src="@/static/index/search.png"></image>
						<text>请输入搜索关键词</text>
					</view>
				</view>
			</u-navbar>
		</view>
		<view class="flex-space-between">
			<view class="left-box">
				<scroll-view scroll-y :style="{ 'height': scrollHeight + 'px' }">
					<view class="filter-box">
						<view v-for="(item, index) of categoryList" :key="index"
							:class="['filter', tabIndex == index ? 'is-filter' : '']" @click="tab(item, index)">
							{{ item.categoryName }}
						</view>
					</view>
				</scroll-view>
			</view>
			<view class="right-box">
				 <!-- @scrolltolower="scrolltolower()" -->
				 <!-- :scroll-top="pageScrollTo" -->
				<scroll-view scroll-y :style="{ 'height': scrollHeight + 'px' }"  :scroll-into-view="scrollIntoViewId" :scroll-with-animation="true"  >
					<view class="container">
						<view class="title m-b-20"> </view>
						<view v-for="(categoryItem, categoryIndex) of categoryList" :key="categoryIndex">
							<view class="title m-b-20" :id=" 'category' + categoryIndex">{{categoryItem.categoryName}}</view>
							<view class="good-box">
								<view class="good" v-for="(item,index) of categoryItem.secondCategoryResults" :key="index" @click="searchList(item)">
									<image class="good-img " mode="aspectFit" :src="item.categoryUrl"></image>
									<view class="introduce-box">
										<view class="name m-b-20">{{ item.categoryName }}</view>
									</view>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script>
import {
	categorySearch,
	goodsList,
} from '@/common/api/index';
export default {
	data() {
		return {
			tabIndex: 0,
			scrollHeight: 0,
			categoryList: [],
			categoryId: 0,
			shoppList: [],
			shopproductinfoPage: {
				page: 0,
				size: 10,
			},
			scrollIntoViewId:'category1',
			endPage: false,
		}
	},
	onLoad(options) {
		this.categorySearch()
	},
	onReady() {
		this.systemHeight()
	},
	methods: {
		async systemHeight() {
			let navbar = await this.$utils.getWindHeight('#navbar')
			let tabBarHeight = 0;
			// uni.getSystemInfo({
			//   success: function (info) {
			//     // 如果底部 tabBar 是常规样式（safeAreaInsets.bottom 不为0），则需要加上安全区域的高度
			//      tabBarHeight = info.safeAreaInsets.bottom;
			//     if (tabBarHeight === 0) {
			//       // 如果不是在安全区域内，可能是固定高度或者自定义tabbar
			//       tabBarHeight = 49; // 假设默认高度是49px，根据实际情况可能需要调整
			//     }
			//   }
			// });
			this.scrollHeight = navbar
		},

		// 分类
		categorySearch() {
			categorySearch({
			}).then(res => {
				this.categoryList = res.data.list
				this.categoryId = this.categoryList[0].id
				// this.goodsLists()
			}).catch(err => { });
		},

		// 商品列表接口
		// goodsLists() {
		// 	goodsList({
		// 		...this.shopproductinfoPage,
		// 		categoryId: this.categoryId
		// 	}).then(res => {
		// 		if (res.code === 200) {
		// 			this.shoppList = this.shoppList.concat(res.data.list || [])
		// 			this.endPage = res.data.endPage
		// 		}
		// 	})
		// },

		//搜索
		search() {
			uni.navigateTo({
				url: `/subPackages/index/search/index`
			})
		},

		// 分类
		tab(item, index) {
			if (this.tabIndex == index) return
			this.tabIndex = index
			
			// 假设在某个事件或方法中
			// const anchor = 'category' + index; // 对应前面定义的id
			// uni.createSelectorQuery().select('#' + anchor).boundingClientRect(data => {
			//  console.log('data.top=========>',data.top)
			//   this.pageScrollTo = data.top - 64
			// }).exec();
			
			this.scrollIntoViewId =  'category' + index;
			
			// this.categoryId = item.id
			// this.shopproductinfoPage.page = 0
			// this.shoppList = []
			// this.endPage = false
			// this.goodsLists()
		},

		// 商品列表事件
		goodDetailed(item) {
			uni.navigateTo({
				url: `/subPackages/index/goodDetailed/index?id=${item.id}`
			})
		},

		// 上拉加载
		// scrolltolower() {
		// 	if (this.endPage) return
		// 	this.shopproductinfoPage.page++
		// 	this.goodsLists()
		// },
		
		searchList(item){
			uni.navigateTo({
				url: `/subPackages/index/searchList/index?brandName=${item.categoryName}&categoryId=${item.id}`
			})
		}
	}
}
</script>

<style lang="scss" scoped>
::v-deep .slot-wrap {
	flex: 1;
	z-index: 99999999;
}

#navbar {
	background: #ffffff;
}

.search {
	flex: 1;
	height: 70rpx;
	margin: 36rpx 50rpx 20rpx 30rpx;
	background: #F8F8F8;
	border-radius: 40rpx;
	font-family: PingFangSC, PingFang SC;
	font-weight: 500;
	font-size: 30rpx;
	color: #CCCCCC;
	display: flex;
	align-items: center;
	padding: 0rpx 23rpx;
	box-sizing: border-box;
	z-index: 99999999;

	image {
		width: 29rpx;
		height: 29rpx;
		margin-right: 20rpx;
	}
}

.left-box {
	width: 190rpx;
	background: #ffffff;

	.filter-box {
		.filter {
			width: 190rpx;
			height: 100rpx;
			line-height: 100rpx;
			text-align: center;
			font-family: PingFangSC, PingFang SC;
			font-weight: 600;
			font-size: 30rpx;
			color: #777777;
			position: relative;
		}

		.is-filter {
			color: #222222;
			background: #F8F8F8;
		}

		.is-filter:after {
			content: '';
			background: #C11C2F;
			position: absolute;
			width: 8rpx;
			height: 40rpx;
			left: 4rpx;
			top: 0;
			bottom: 0;
			margin: auto;
		}

		.price {
			display: flex;
			align-items: center;
			justify-content: center;

			.arrow-box {
				display: flex;
				flex-direction: column;
			}
		}
	}

}

.right-box {
	width: 560rpx;

	.container {
		padding: 20rpx;
		box-sizing: border-box;

		.good-box {
			display: flex;
			flex-wrap: wrap;
			justify-content: space-between;

			.good {
				width: 250rpx;
				padding-bottom: 30rpx;
				box-sizing: border-box;
				background: #ffffff;
				margin-bottom: 20rpx;
				border-radius: 16rpx;
				overflow: hidden;
				display: flex;
				flex-direction:column;

				.good-img {
					width: 100%;
					height: 250rpx;
				}

				.introduce-box {
					flex: 1;
					padding: 10rpx 20rpx 0;
					box-sizing: border-box;
					display: flex;
					flex-direction:column;
					justify-content: space-between;

					.name {
						font-family: PingFangSC, PingFang SC;
						font-weight: 600;
						font-size: 30rpx;
						color: #222222;
						text-align: left;
						font-style: normal;
						word-wrap: break-word;
						word-break: normal;
					}

					.price {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 22rpx;
						color: #9E1115;
						font-style: normal;
					}

					.sold {
						font-family: PingFangSC, PingFang SC;
						font-weight: 500;
						font-size: 22rpx;
						color: #777777;
						font-style: normal;
					}
				}
			}

		}
	}

}
</style>